﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Company_CompetePrice : System.Web.UI.Page, IAccess
{
    string action = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        // 验证报价密码
        action = (Request["action"] ?? "compete").ToLower();
        if (!CurrentAccount.IsCheckBidPwd)
        {
            Response.Redirect("CheckBidPwd.aspx?action=compete");
        }


        if (!CurrentProject.IsCompeteSwitch)
        {
            HZ.Web.MessageBox.Show("竞价开关未开启，改功能暂时无法使用！");
        }

        if (CurrentProject.InBidTime < 0)
        {
            HZ.Web.MessageBox.Show("报价时间未到，请稍后！");
        }

        if (CurrentProject.InBidTime > 0)
        {
            HZ.Web.MessageBox.Show("报价时间已到！");
        }

        if (CurrentAccount.IsConfirmBid)
        {
            Response.Redirect(string.Format("PrePrintPrice.aspx?action={0}", action));
        }
        if (!IsPostBack)
        {
            bidinfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("CompeteDes").Value;
            BindData();
            ViewState["CompanyId"] = CurrentAccount.Account.AccountId.Value;
        }
        if (IsPostBack)
        {
            if ((int)ViewState["CompanyId"] != CurrentAccount.Account.AccountId.Value)
            {

                Response.Redirect("~/Timeout.htm");
            }
        }
    }
    protected void btnSubmitPrice_Click(object sender, EventArgs e)
    {
        IList<int> bidids = new List<int>();
        IList<double> bidprices = new List<double>();
        foreach (GridViewRow item in gvbidprice.Rows)
        {
            if (item.RowType == DataControlRowType.DataRow)
            {
                string strprice = (item.FindControl("txtBidPrice") as TextBox).Text;
                if (HZ.Fun.IsUFloat(strprice))
                {
                    var bidid = new HZ.Common.ConvertHelper(item.Cells[0].Text).ToInt32;
                    if (bidid == null)
                    {
                        continue;
                    }
                    else
                    {
                        bidids.Add(bidid.Value);
                        bidprices.Add(new HZ.Common.ConvertHelper(strprice).ToDouble.Value);
                    }
                }
                else
                {
                    continue;
                }
            }
        }
        if (bidids.Count > 0)
        {
            // BLL_Factory.IBLL_Company.BidPrice(bidids, bidprices);
            BLL_Factory.IBLL_Company.CompetePrice(bidids, bidprices, CurrentAccount.CurrentCompeteTimes);
        }

        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pager1.CurrentPage = 1;
        BindData();
    }
    protected void BindData()
    {
        int totalrecord;
        var bidprice = BLL_Factory.IBLL_Company.GetCompeteWithDec(new { CompanyId_Tb = CurrentAccount.Account.AccountId, SortFields = "Company_Goods", CompeteTimes = CurrentAccount.CurrentCompeteTimes, ProductName = txtProdcutName.Text, Model = txtMedicineModel.Text, Outlookc = txtOutlookc.Text, CompanyName_Sc = txtCompanyName_Sc.Text }, pager1.CurrentPage, pager1.PageSize, out totalrecord, CurrentAccount.BidPwd, CurrentAccount.RandPwd);
        pager1.RecordCount = totalrecord;

        if (bidprice.Count == 0)
        {
            btnSubmitPrice.Enabled = false;
        }
        else
        {
            btnSubmitPrice.Enabled = true;
        }
        gvbidprice.DataSource = bidprice;
        gvbidprice.DataBind();

    }

    protected void pager1_PageChanging(object sender, HZ.Web.PageChaningEventArgs e)
    {
        BindData();
    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Company);
            return list;
        }

    }

    #endregion
}
